define(function(require, exports){
	var $ = require("jquery");
	var pubjs = require('pubjs');
	var view = require('@base/view');
	var util = require('util');

	var Main = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				url: 'category/list'
				,urlRemove: 'category/remove'
				,urlClear: 'category/notifyClear'
			});
			this.Super('init', arguments);
		}
		,afterBuild:function(){
			var self = this;
			var c = self.getConfig();
			var el = self.getDOM();

			this.append($([
				'<div class="M-formHead">',
					'<span class="M-formHeadTitle">'+LANG('分类表')+'</span>',
					'<div class="mb10">',
						'<span class="" pub-name="importFile" pub-mod="@base/common/input.button" pub-auth="master.sunfei,category.import"/>',
				'<span class="fr" pub-name="batchRemove" pub-mod="@base/common/input.button" pub-auth="master.sunfei"/>',
					'</div>',
					'<div class="mb10">',
						'<span class="" pub-name="add" pub-mod="@base/common/input.button" pub-auth="master.sunfei"/>',
						'<span class="fr" pub-name="generate" pub-mod="@base/common/input.button" pub-auth="master.sunfei,category.generate"/>',
					'</div>',
				'</div>',
				'<div class="cl" pub-name="grid" pub-mod="category/grid/list.base"/>'
			].join('')));


			this.buildFromTemplate({
				importFile: {
					value: '上传分类文件进行导入'
					,'class': 'btn btn-primary'
					,width: null
				}
				,generate: {
					value: '生成分类文件'
					,'class': 'btn btn-primary'
					,width: null
				}
/*				,clear: {
					value: '通知清除DSP的缓存'
					,'class': 'btn btn-danger'
					,width: null
				}*/
				,add: {
					value: '单个分类添加'
					,'class': 'btn btn-success'
					,width: null
				}
/*				,batchInner: {
					value: '批量导入系统内部已定义好的分类'
					,'class': 'btn btn-primary'
					,width: null
				}*/
				,batchRemove: {
					value: '批量删除分类'
					,'class': 'btn btn-danger'
					,width: null
				}
				,grid: {
					'hasSearch': true,
					'hasFilter': true
				}
			});

			self.createAsync('slotSidebar', '@layout/v1/sidebar.base', {
				'target': c.targetSidebar,
				'items': [
					{'name': 'channel', 'uri':'modules/sideBarBase.channel','config':{'monitorUrl':''}},
					{'name': 'type', 'uri':'modules/sideBarBase.radio', 'config': {
						'title': LANG('分类：'),
						'data': pubjs.config('categories') || [],
						'key': '_id',
						'all': true
					}}
					,{'name': 'IsDeleted', 'uri':'modules/sideBarBase.radio', 'config': {
						'title': LANG('模板隐藏：'),
						'data': [
							{id: 0, name: LANG('正常')},
							{id: 1, name: LANG('隐藏')}
						],
						'value': 0
					}}
				]
			});

		}
		,onInputClick: function(ev) {
			var url = '';
			switch (ev.name) {
				case 'clear':
					pubjs.loading.show();
					return pubjs.mc.send(this.getConfig('urlClear'), function(err, data) {
						pubjs.loading.hide();
						if(err) {
							return pubjs.alert(err.message);
						}

						pubjs.alert(data && data.message || LANG('通知成功'));
					});
				case 'importFile':
					url = 'category/import';
					break;
				case 'add':
					url = 'category/edit';
					break;
				case 'generate':
					url = 'category/generate';
					break;
				case 'batchRemove':
					url = 'category/batchRemove';
					break;
				case 'batchInner':
					url = 'category/batchImport';
					break;
				default :
					return false;
			}

			pubjs.controller.navigate(url);
		}
		,onSidebarFilter: function(ev){
			var self = this
				,param = {};

			util.each(ev.param, function(item, idx){
				var type = idx;
				var id = item.id;

				switch (type) {
					case 'type':
						param.Type = id;
						break;
					case 'channel':
						param.ChannelId = +id;
						break;
				}
			});

			self.$.grid.setParam(param).load();
		}
		,reload: function() {
			this.$.grid.load();
		}
		,onListOpClick:function(ev){
			//编辑单条记录
			var self = this;
			var c = self.getConfig();
			if(ev.param.op == 'edit') {
				pubjs.controller.navigate('category/edit/' + ev.param.data._id);
			}
			else if(ev.param.op == 'del') {
				pubjs.confirm(LANG('确认要删除此记录吗, 此删除是不可恢复的, 如有子类也会随之删除'), function(res) {
					if(res) {
						pubjs.mc.send(c.urlRemove, {id: ev.param.data._id}, function(err) {
							if(err) {
								pubjs.alert(err.message);
								return false;
							}

							pubjs.alert(LANG('删除成功'));
							self.reload();
						});
					}
				});
			}
			return false;
		}
	});

	exports.main = Main;
});